package com.kangye.learnteacher.controller;

import com.github.pagehelper.PageHelper;
import com.kangye.learnteacher.entity.QuestionBank;
import com.kangye.learnteacher.service.QuestionBankService;
import com.kangye.learnteacher.util.DownloadFileUtil;
import com.kangye.learnteacher.util.ExcelImportUtils;
import com.kangye.learnteacher.util.RedisStorageUtil;
import com.kangye.learnteacher.util.ViewJsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: learndeduo
 * @description: 老师端题库请求接口
 * @author: 苗伟晨
 * @create: 2021-05-17 17:37
 **/
@RestController
@CrossOrigin
@RequestMapping("/questionBank")
public class QuestionBankController {

    @Autowired
    private QuestionBankService questionBankService;
    @Autowired
    private RedisStorageUtil redisStorageUtil;
    @Autowired
    private DownloadFileUtil downloadFileUtil;
    @Autowired
    private ExcelImportUtils excelImportUtils;
    /**
     * 查询全部题目
     * */
    @RequestMapping("/queryAll")
    public ViewJsonObject<Map<String,Object>> queryAllQb(
            @RequestParam(value = "page",defaultValue = "1")Integer page,@RequestParam(value = "limit",defaultValue = "1") Integer limit
            ,Integer qb_id,String subject_name,Integer gradeId,String type_name){
        ViewJsonObject<Map<String,Object>> list = questionBankService.queryAllQuestionBank(page,limit,qb_id,subject_name,gradeId,type_name);
        return list;
    }

    /**
     * 根据id删除题目
     * */
    @RequestMapping("/delById")
    public Map<String,Object> delById(Integer qb_id){
        return questionBankService.deleteById(qb_id);
    }

    /**
     * 查询题型
     * */
    @RequestMapping("/queryQuestion")
    public ViewJsonObject<QuestionBank> querySubject(QuestionBank questionBank){
        ViewJsonObject<QuestionBank> querySubject = questionBankService.querySubject(questionBank);
        return querySubject;
    }

    /**
     * 根据id进行查找
     * */
    @RequestMapping("/queryById")
    public List<Map<String,Object>> queryById(Integer qb_id){
        List<Map<String,Object>> queryById = questionBankService.queryById(qb_id);
        return queryById;
    }

    /**
     * 从redis中获取到数据
     * */
    @RequestMapping("/showAll")
    public ViewJsonObject showAll(){
        //在redis中取到数据
        List<QuestionBank>  queryByIds = (List<QuestionBank>) redisStorageUtil.getKeyOne("queryById");
        return new ViewJsonObject(queryByIds);
    }

    /**
     * 通过id修改
     * */
    @RequestMapping("/modifyById")
    public Integer modifyById(Integer qb_id,String qb_name,String type_name,double qb_score,Integer qb_difficulty,String subject_name,String grade_name,String qb_answer){
        return null;
    }
    /**
     * 下载模板
     * */
    @RequestMapping("/down")
    public void down(HttpServletResponse resp){
        ClassPathResource cpr = new ClassPathResource("/questionBank.xlsx");
        String fileName = "题库导入模板.xlsx";
        downloadFileUtil.download(cpr, fileName, resp);
    }
    /**
     * 上传模板
     * */
    @RequestMapping("/upload")
    public Object upload(MultipartFile file){
        //判断文件是否为空
        if (file == null) {
            return "文件不能为空";
        }

        //获取文件名
        String fileName = file.getOriginalFilename();
        System.out.println("fileName:"+fileName+";file:"+file);
        //验证文件名是否合格
        if (!excelImportUtils.validateExcel(fileName)) {
            return "文件类型错误!请重新下载模板";
        }

        //进一步判断文件内容是否为空（即判断其大小是否为0或其名称是否为null）
        long size = file.getSize();
        if (fileName.isEmpty() || size == 0) {
            return "文件不能为空";
        }

        //批量导入
        String message = questionBankService.batchImport(fileName, file);
        Map<String,Object> map =new HashMap<>();
        map.put("msg",message);
        return map;
    }
}

